


class Solution {
    public ListNode partition(ListNode head, int x) {
      ListNode minHead = new ListNode() ;
      ListNode minEnd = minHead;
       ListNode maxHead = new ListNode() ;
      ListNode maxEnd =  maxHead;
       ListNode cur = head ;
       while(cur!=null){
         if(cur.val < x){
             minEnd.next = cur ;
             minEnd = cur ;
             cur = cur.next ;
         }else{
            maxEnd.next = cur ;
            maxEnd = cur ;
            cur = cur.next ;
         }
       }
       maxEnd.next = null ;
       minEnd.next = maxHead.next ;
         return minHead.next ;
    }
}